import React, { Component } from 'react'
import PropTypes from 'prop-types';
import './select.scss'

class SelectItem extends Component {
  constructor (props) {
    super(props)
    this.selectItemActive = this.selectItemActive.bind(this)
  }
  parent () {
    return this.context.component
  }
  selectItemActive () {
    this.parent().setValue(this.props)
  }
  componentDidMount () {
    if (this.parent().props.value === this.props.value) {
      this.parent().setValue(this.props)
    }
  }
  render () {
    return (
      <div onClick={this.selectItemActive} className="selectItem">
        { this.props.label }
      </div>
    )
  }
}

SelectItem.contextTypes = { // 定义了contextTypes，this.context才有值
  component: PropTypes.any
};

SelectItem.propTypes = {
  value: PropTypes.any,
  label: PropTypes.string
}

export default SelectItem